#pragma once


#include <stdint.h>
#include <string>
#include <memory>
#include <mysql/mysql.h>
#include "MySqlConf.h"

namespace db 
{

class MySqlConn
{
public:
    using ptr = std::shared_ptr<MySqlConn>;

    MySqlConn(const std::string& charset = "utf8");
    ~MySqlConn();
    bool connect(MySqlConf conf);
    bool connect(const std::string& db
                , const std::string& user, const std::string& password
                , const std::string& ip = "", uint16_t port = 3306);
    bool excute(const std::string& sql);
    unsigned int row();
    unsigned int column();
    bool next();
    std::string name(unsigned int index);
    std::string value(unsigned int index);
    //std::string value(unsigned int line, unsigned int column);
    bool transcation();
    bool commit(); 
    bool rollback();
    bool setCharSet(const std::string& charset = "utf8");
    bool isConnected() { return m_mysql != nullptr; }

    unsigned int errorno();
    std::string errmsg();

private:
    void freeResult();

private:
    MYSQL *m_mysql = nullptr;
    MYSQL_RES* m_result = nullptr;
    MYSQL_ROW m_row = nullptr;
};

} // namespace db 